package com.sunchen.asc.application.rest;

import com.alibaba.fastjson.JSONObject;
import com.sunchen.asc.service.common.ResultResponse;
import com.sunchen.asc.service.common.ResultUtil;
import com.sunchen.asc.service.user.api.RoleService;
import com.sunchen.asc.service.user.dto.RoleDto;
import com.sunchen.asc.service.user.vo.InsertRoleVo;
import com.sunchen.asc.service.user.vo.RoleVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleRest {

    @DubboReference
    private RoleService roleService;

    @ApiOperation(value = "更新resource")
    @RequestMapping(value = "/resource/update",method = RequestMethod.GET)
    public ResultResponse resup(){

        return roleService.resourceUpdate();
    }

    @ApiOperation(value = "用户权限节点展示")
    @RequestMapping(value = "/auth/get",method = RequestMethod.GET)
    public ResultResponse getAuthentication(){

        return roleService.getAuthentication();
    }

    @ApiOperation(value = "用户角色展示")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public ResultResponse getRoles(){

        return roleService.getRoles();
    }

    @ApiOperation(value = "url列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="title",value = "resource名称",required = false),
            @ApiImplicitParam(name="page",value = "页码",dataType = "Integer",required = false,defaultValue = "1"),
            @ApiImplicitParam(name="limit",value = "每页显示条数",dataType = "Integer",required = false,defaultValue = "10"),
            @ApiImplicitParam(name="order",value = "排序字段",dataType = "String",required = false,defaultValue = "id"),
            @ApiImplicitParam(name="orderType",value = "升序/降序",dataType = "String",required = false,defaultValue = "asc")
    })
    @RequestMapping(value = "/url/query",method = RequestMethod.GET)
    public ResultResponse urlQuery(@RequestParam(required = false,defaultValue = "") String title,
                                   @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                   @RequestParam(value = "limit",defaultValue = "10",required = false) Integer limit,
                                   @RequestParam(value = "order",defaultValue = "id",required = false) String order,
                                   @RequestParam(value = "orderType",defaultValue = "asc",required = false) String orderType){

        Sort sort = Sort.by(Sort.Direction.fromString(orderType),order);
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        return roleService.urlQuery(title,pageable);
    }

    @ApiOperation(value = "根据url获取角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "url_id",required = true)
    })
    @RequestMapping(value = "/url/role/get",method = RequestMethod.GET)
    public ResultResponse getRoleByUrl(@RequestParam Long id){

        return roleService.getRoleByUrl(id);
    }

    @ApiOperation(value = "角色分配url")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "角色id",required = true),
            @ApiImplicitParam(name="urlIds[]",value = "路径id集合",required = true)
    })
    @RequestMapping(value = "/url/set",method = RequestMethod.POST)
    public ResultResponse addRoleUrl(@RequestParam Long id, @RequestParam(value = "urlIds[]") List<Long> urlIds){

        return roleService.addRoleUrl(id,urlIds);
    }

    @ApiOperation(value = "url展示")
    @RequestMapping(value = "/url/get",method = RequestMethod.GET)
    public ResultResponse urlList(){

        return roleService.urlList();
    }

    @ApiOperation(value = "获取角色详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "角色id",dataType = "Long",required = true)
    })
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ResultResponse getRoleDetail(@RequestParam Long id){

        return roleService.getRoleDetail(id);
    }

    @ApiOperation(value = "用户权限节点新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "名称",dataType = "String",required = true),
            @ApiImplicitParam(name="title",value = "描述",dataType = "String",required = true)
    })
    @RequestMapping(value = "/auth/create",method = RequestMethod.GET)
    public ResultResponse creatAuthentication(@RequestParam String name,
                                              @RequestParam String title){

        return roleService.creatAuthentication(name,title);
    }

    @ApiOperation(value = "用户权限节点编辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "权限id",dataType = "Long",required = true),
            @ApiImplicitParam(name="name",value = "名称",dataType = "String",required = true),
            @ApiImplicitParam(name="title",value = "描述",dataType = "String",required = true)
    })
    @RequestMapping(value = "/auth/edit",method = RequestMethod.GET)
    public ResultResponse editAuthentication(@RequestParam Long id,
                                             @RequestParam String name,
                                             @RequestParam String title){

        return roleService.editAuthentication(id,name,title);
    }

    @ApiOperation(value = "用户权限节点删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "权限id",dataType = "Long",required = true)
    })
    @RequestMapping(value = "/auth/delete",method = RequestMethod.GET)
    public ResultResponse deleteAuthentication(@RequestParam Long id){

        return roleService.deleteAuthentication(id);
    }

    @ApiOperation(value = "权限查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="title",value = "名称",dataType = "String",required = false),
            @ApiImplicitParam(name="page",value = "页码",dataType = "Integer",required = false,defaultValue = "1"),
            @ApiImplicitParam(name="limit",value = "每页显示条数",dataType = "Integer",required = false,defaultValue = "10"),
            @ApiImplicitParam(name="order",value = "排序字段",dataType = "String",required = false,defaultValue = "id"),
            @ApiImplicitParam(name="orderType",value = "升序/降序",dataType = "String",required = false,defaultValue = "asc")
    })
    @RequestMapping(value = "/auth/list",method = RequestMethod.GET)
    public ResultResponse authList(@RequestParam(value = "title",defaultValue = "",required = false) String title,
                                   @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                   @RequestParam(value = "limit",defaultValue = "10",required = false) Integer limit,
                                   @RequestParam(value = "order",defaultValue = "id",required = false) String order,
                                   @RequestParam(value = "orderType",defaultValue = "asc",required = false) String orderType){

        Sort sort = Sort.by(Sort.Direction.fromString(orderType),order);
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        return roleService.authList(pageable,title);
    }

    @ApiOperation(value = "用户角色查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "名称",dataType = "String",required = false),
            @ApiImplicitParam(name="page",value = "页码",dataType = "Integer",required = false,defaultValue = "1"),
            @ApiImplicitParam(name="limit",value = "每页显示条数",dataType = "Integer",required = false,defaultValue = "10"),
            @ApiImplicitParam(name="order",value = "排序字段",dataType = "String",required = false,defaultValue = "id"),
            @ApiImplicitParam(name="orderType",value = "升序/降序",dataType = "String",required = false,defaultValue = "asc")
    })
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultResponse roleList(@RequestParam(value = "name",defaultValue = "",required = false) String name,
                                   @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                   @RequestParam(value = "limit",defaultValue = "10",required = false) Integer limit,
                                   @RequestParam(value = "order",defaultValue = "id",required = false) String order,
                                   @RequestParam(value = "orderType",defaultValue = "asc",required = false) String orderType){

        Sort sort = Sort.by(Sort.Direction.fromString(orderType),order);
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        Page<RoleVo> list = roleService.roleList(pageable,name);
        return ResultUtil.success(list);
    }

    @ApiOperation(value = "用户角色新增")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public ResultResponse creatRole(@RequestBody @ApiParam(name="角色vo对象",value="传入json格式",required=true) InsertRoleVo insertRoleVo){

        RoleDto roleDto = new RoleDto(insertRoleVo);
        return roleService.creatRole(roleDto);
    }

    @ApiOperation(value = "用户角色编辑")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ResultResponse editRole(@RequestBody @ApiParam(name="角色vo对象",value="传入json格式",required=true) InsertRoleVo insertRoleVo){
        RoleDto roleDto = new RoleDto(insertRoleVo);
        return roleService.editRole(roleDto);
    }

    @ApiOperation(value = "用户角色删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "角色id",dataType = "Long",required = true)
    })
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultResponse deleteRole(@RequestParam Long id){

        return roleService.deleteRole(id);
    }

    @ApiOperation(value = "用户角色启用/禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "角色id",dataType = "Long",required = true),
            @ApiImplicitParam(name="status",value = "状态（1：开启2：禁用）",dataType = "Integer",required = true)
    })
    @RequestMapping(value = "/status/set",method = RequestMethod.GET)
    public ResultResponse setStatus(@RequestParam Long id,@RequestParam int status){

        return roleService.setStatus(id,status);
    }

    @ApiOperation(value = "表单验证角色名是否唯一")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "角色名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/onlyname/create",method = RequestMethod.GET)
    public JSONObject findTheOnlyRoleNameCreat(@RequestParam String name){

        return roleService.findTheOnlyNameCreat(name);
    }

    @ApiOperation(value = "表单验证修改时角色名是否唯一")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "角色id",dataType = "Long",required = true),
            @ApiImplicitParam(name="name",value = "角色名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/onlyname/modify",method = RequestMethod.GET)
    public JSONObject findTheOnlyRoleNameModify(@RequestParam String name,@RequestParam Long id){

        return roleService.findTheOnlyNameModify(name,id);
    }

    @ApiOperation(value = "表单验证权限名是否唯一")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "权限名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/auth/onlyname/create",method = RequestMethod.GET)
    public JSONObject findTheOnlyAuthNameCreat(@RequestParam String name){

        return roleService.findTheOnlyAuthNameCreat(name);
    }

    @ApiOperation(value = "表单验证修改时权限名是否唯一")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "权限id",dataType = "Long",required = true),
            @ApiImplicitParam(name="name",value = "权限名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/auth/onlyname/modify",method = RequestMethod.GET)
    public JSONObject findTheOnlyAuthNameModify(@RequestParam String name,@RequestParam Long id){

        return roleService.findTheOnlyAuthNameModify(name,id);
    }

}
